

**********************************************************************


clear all
set maxvar 120000 


cap noisily cd "/Users/aspearot/Dropbox/MaLT 2017/Draft/submission_draft/ReStat/resubmission 2/replication package/malt/"

use "Data/Kilimanjaro Farmer_mktaccess.dta", clear


		use "Data/Manyara Farmer_mktaccess.dta", clear
			drop secf*
			rename (survey_district survey_ward survey_village) (district ward village_name)
			tempfile manyarafarmer
			save `manyarafarmer'
		
		use "Data/Kilimanjaro Farmer_mktaccess.dta", clear
			
			merge m:1 village_name ward district using "Data/Kilimanjaro Census.dta"
			keep if _m==3
			rename census_numhh_pervillage numhh_pervillage
			
			append using `manyarafarmer'

	*create location group
	egen location = group(district ward village_name)
			
	gen log_land = log(farmer_acres_land)
	gen log_harv = log(harvest_kg_w5)  

	local farmer_controls "farmer_age_r farmer_female_r farmer_married_r farmer_years_education_r farmer_iron_roof_r farmer_not_mud_walls_r farmer_mud_floor_r farmer_bank_account_r farmer_mobile_money_r farmer_hh_size_r farmer_mobile_phone_r farmer_pc1_durables_r farmer_pc1_animals_r farmer_has_business_r farmer_total_income_USD_r farmer_age_m farmer_female_m farmer_married_m farmer_years_education_m farmer_iron_roof_m farmer_not_mud_walls_m farmer_mud_floor_m farmer_bank_account_m farmer_mobile_money_m farmer_hh_size_m farmer_mobile_phone_m farmer_pc1_durables_m farmer_pc1_animals_m farmer_has_business_m farmer_total_income_USD_m"


	*columns 1 and 2 of Appendix Table B1
	logit used_fert log_land i.location 
	*outreg2 log_land using "Results/Logit.xls", drop(i.location) excel replace

	logit used_fert log_land i.location `farmer_controls'
	*outreg2 log_land farmer_controls using "Results/Logit.xls", drop(i.location) excel 
	predict adopt_pred

	gen adopt_index = log_land*_b[log_land]+farmer_age_r*_b[farmer_age_r]+farmer_female_r*_b[farmer_female_r]+farmer_married_r*_b[farmer_married_r]+farmer_years_education_r*_b[farmer_years_education_r]+farmer_iron_roof_r*_b[farmer_iron_roof_r]+farmer_not_mud_walls_r*_b[farmer_not_mud_walls_r]+farmer_mud_floor_r*_b[farmer_mud_floor_r]+farmer_bank_account_r*_b[farmer_bank_account_r]+farmer_mobile_money_r*_b[farmer_mobile_money_r]+farmer_hh_size_r*_b[farmer_hh_size_r]+farmer_mobile_phone_r*_b[farmer_mobile_phone_r]+farmer_pc1_durables_r*_b[farmer_pc1_durables_r]+farmer_pc1_animals_r*_b[farmer_pc1_animals_r]+farmer_has_business_r*_b[farmer_has_business_r]+farmer_total_income_USD_r*_b[farmer_total_income_USD_r]+farmer_age_m*_b[farmer_age_m]+farmer_female_m*_b[farmer_female_m]+farmer_married_m*_b[farmer_married_m]+farmer_years_education_m*_b[farmer_years_education_m]+farmer_iron_roof_m*_b[farmer_iron_roof_m]+farmer_not_mud_walls_m*_b[farmer_not_mud_walls_m]+ farmer_mud_floor_m*_b[farmer_mud_floor_m]+farmer_bank_account_m*_b[farmer_bank_account_m]+farmer_mobile_money_m*_b[farmer_mobile_money_m]+ farmer_hh_size_m*_b[farmer_hh_size_m]+farmer_mobile_phone_m*_b[farmer_mobile_phone_m]+farmer_pc1_durables_m*_b[farmer_pc1_durables_m]+ farmer_pc1_animals_m*_b[farmer_pc1_animals_m]+farmer_has_business_m*_b[farmer_has_business_m]+farmer_total_income_USD_m*_b[farmer_total_income_USD_m]

	egen adopt_index_avg = mean(adopt_index), by(location) 

	*Impute values for villages with all ones or all zeros.  Normalize village means to a large or small positive number, and then create farmer level variation using the estimated coefficient.
	
	egen adopt_avg = mean(used_fert), by(location)
	replace adopt_avg = 0.001 if adopt_avg==0
	replace adopt_avg = 0.999 if adopt_avg==1	

	replace adopt_pred=exp(log(adopt_avg/(1-adopt_avg))+(adopt_index-adopt_index_avg))/(1+exp(log(adopt_avg/(1-adopt_avg))+(adopt_index-adopt_index_avg)))

	gen log_adopt_pred = log(adopt_pred)

	*Here we construct a measure of fertilizer expenditures based on purchases of DAP and UREA 
	
		gen expense_pre = fert_expense_dap17_imput_USD+fert_expense_urea17_imput_USD
		replace expense_pre = 0 if used_fert==0

		gen expense = fert_expense_dap17_imput_USD+fert_expense_urea17_imput_USD
		replace expense = 0 if used_fert==0
		
	
	*Next we predict expenditures by using a poisson model and regressing on log land, farmer controls (not including land) and the log adoption probability from earlier.
	
	*columns 3 and 4 of Appendix Table B1
	
		poisson expense log_land `farmer_controls' i.location, offset(log_adopt_pred)
		*outreg2 log_land `farmer_controls' using "Results/Expend.xls", drop(i.location) excel replace

		predict expense_pred_offset, n

		poisson expense log_land log_adopt_pred `farmer_controls' i.location
		*outreg2 log_land `farmer_controls' using "Results/Expend.xls", drop(i.location) excel

	*Collect predictions for the second model, which are used in the paper
		
		predict expense_pred, n
	
	*As described in the manuscript, we only use these predictions when expenditures are zero or not reported in the first place:
	
	replace expense_pred = expense_pre*adopt_pred if expense_pre>0&expense_pre!=.
	
	
*save dataset to use with counterfactuals in R

keep survey_region district ward village_name market farmer_acres_land google_vil_city_km used_fert adopt_pred adopt_index expense expense_pred expense_pre `farmer_controls'
 
save "Data/MaLT Calibration Replication Alt.dta", replace


